<meta charset=utf-8>
<style>
body { margin: 0px; }
canvas { width:100%; height:100%; overflow: hidden; }
</style>
<head><meta name="viewport" content="user-scalable=no,initial-scale=1,maximum-scale=1">
<script type="text/javascript" src="../h3du_min.js"></script>
<script type="text/javascript" src="../extras/frame.js"></script>
<script type="text/javascript" src="demoutil.js"></script>
<script type="text/javascript" src="../extras/meshjson.js"></script>
</head>
<body>
<canvas id=canvas></canvas>
<script id="demo">
/* global H3DU */
// <!--
/*
 Any copyright to this file is released to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/
 If you like this, you should donate
 to Peter O. (original author of
 the Public Domain HTML 3D Library) at:
 http://peteroupc.github.io/
*/

function setStarPos(star, range) {
  "use strict";
  var x = 0,
    y = 0,
    z = 0;
  var centerZone = range / 20;
   // avoid stars too close to the center
  while(Math.abs(x) < centerZone) {
    x = Math.random() * range - range / 2;
  }
   // avoid stars too close to the center
  while(Math.abs(y) < centerZone) {
    y = Math.random() * range - range / 2;
  }
  z = Math.random() * range - range / 2;
  return star.setPosition(x, y, z);
}

function starField(scene, range) {
  "use strict";
  var group = new H3DU.ShapeGroup();
  // use a crude white sphere to represent a star
  var star = new H3DU.Shape(H3DU.Meshes.createSphere(range / 1000, 4, 3)).setColor("white");
  for(var i = 0; i < 200; i++) {
    group.addShape(setStarPos(star.copy(), range));
  }
  return group;
}

function moveStarField(group, range, frames) {
  "use strict";
  for(var i = 0; i < group.shapeCount(); i++) {
    var pos = group.getShape(i).getMatrix();
    if(pos[14] > range / 2) {
   // once the star is too close, move it elsewhere
      setStarPos(group.getShape(i), range);
      group.getShape(i).getTransform().movePosition(0, 0, -range);
    } else {
      group.getShape(i).getTransform().movePosition(0, 0, range / 250 * frames);
    }
  }
}

  // Create the 3D scene; find the HTML canvas and pass it
  // to Scene3D.
var scene = new H3DU.Scene3D(document.getElementById("canvas"))
   .cullFace(H3DU.Scene3D.BACK);
var sub = new H3DU.Batch3D();
sub.getLights().setBasic();
var fc = new H3DU.FrameCounterDiv();
var timer = {};
var group = starField(scene, 1000);
sub.addShape(group);
H3DU.renderLoop(function(time) {
  "use strict";
  sub.perspectiveAspect(45, 5, 1000).setLookAt([0, 0, 500]);
  moveStarField(group, 1000, H3DU.newFrames(timer, time));
  fc.update();
  scene.render(sub);
});
// -->
</script>
</body>
